
#include <pspkernel.h>
#include <time.h>
#include <stdio.h>
#include "psppower.h"
#include "pspvideo.h"
#include "psptools.h"
#include "bmp.h"

#define FONT_SIZE 0.8f
#define FONT_HEIGHT 16

int printTime(const FORM_HELPER* fh)
{
    time_t time;
    struct tm *t;
    int battery;
    char buf[64];
    unsigned int color;
    RECT src_rect = {0, 0, 16, 16};
    RECT rect = {0, 0, SCR_WIDTH, 20};

    sceKernelLibcTime(&time);
    time += 9 * 60 * 60; /* GMT +9 */
    t = localtime(&time);
    battery = scePowerGetBatteryLifePercent();
    if (battery > 66)
    {
        color = GREEN;
    }
    else if (battery > 33)
    {
        color = YELLOW;
    }
    else
    {
        color = RED;
    }
    videoCopyRect(bar0, &src_rect, &rect);
    mhStart();
    if (fh->total)
    {
        sprintf(buf, "%d", fh->total);
        mhPrint(100, FONT_HEIGHT, buf, RED, FONT_SIZE);
    }
    sprintf(buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec);
    mhPrint(320, FONT_HEIGHT, buf, BLUE, FONT_SIZE);
    sprintf(buf, "%3d%%", battery);
    mhPrint(420, FONT_HEIGHT, buf, color, FONT_SIZE);
    mhEnd();
    return 0;
}

int createEditbox(RECT* rect, int focus, wchar_t* key)
{
    unsigned long bgcolor, txtcolor;

    if (focus)
    {
        bgcolor = WHITE;
        txtcolor = BLACK;
    }
    else
    {
        bgcolor = DARKGRAY;
        txtcolor = GRAY;
    }
    videoFillRect(bgcolor, rect);
    mhStart();
    mhPrintUCS2(rect->left + 5, rect->top + FONT_HEIGHT, key, txtcolor, FONT_SIZE);
    mhEnd();
    return 0;
}

int createButton(RECT* rect, int focus, unsigned long* button)
{
    RECT src_rect = {0, 0, 64, 32};
    unsigned long* bmp;

    if (focus)
    {
        bmp = bmp_button_on;
    }
    else
    {
        bmp = bmp_button_off;
    }
    videoCopyRect(bmp, &src_rect, rect);
    videoCopyRect(button, &src_rect, rect);
    return 0;
}

int listItems(RECT* rect, int focus, const FORM_HELPER* fh)
{
    int i, select;
    unsigned long bgcolor, txtcolor, selcolor;

    if (focus)
    {
        bgcolor = WHITE;
        txtcolor = BLUE;
        selcolor = RED;
    }
    else
    {
        bgcolor = DARKGRAY;
        txtcolor = NAVY;
        selcolor = MAROON;
    }
    videoFillRect(bgcolor, rect);
    select = fh->select - 1;
    mhStart();
    for (i = 0; i < fh->count; i++)
    {
        if (i == select)
        {
            mhPrintUCS2(15, i * 20 + rect->top + FONT_HEIGHT, fh->list[i].title, selcolor, FONT_SIZE);
        }
        else
        {
            mhPrintUCS2(15, i * 20 + rect->top + FONT_HEIGHT, fh->list[i].title, txtcolor, FONT_SIZE);
        }
    }
    mhEnd();
    return 0;
}

#define CTRL_COUNT 5
int searchForm(const FORM_HELPER* fh)
{
    int i = 0;
    int focus[CTRL_COUNT] = {0};
    RECT rect[CTRL_COUNT] = {
        {40, 20, 440, 40}, /* edit box */
        {40, 42, 104, 74}, /* search button */
        {120, 42, 184, 74}, /* search button */
        {200, 42, 264, 74}, /* search button */
        {10, 64, 470, 264}, /* */
    };
    if (fh->focus >= CTRL_COUNT)
    {
        focus[CTRL_COUNT - 1] = 1;
    }
    else if (fh->focus < 0)
    {
        focus[0] = 1;
    }
    else
    {
        focus[fh->focus] = 1;
    }
    videoFillFrame(LIGHTGRAY);
    createEditbox(&rect[i], focus[i], fh->keyword);
    i++;
    createButton(&rect[i], focus[i], bmp_button0);
    i++;
    createButton(&rect[i], focus[i], bmp_button1);
    i++;
    createButton(&rect[i], focus[i], bmp_button2);
    i++;
    listItems(&rect[i], focus[i], fh);
    printTime(fh);
    return 0;
}
